package com.syezon.wifikey.wifi.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.syezon.wifikey.R;
import com.syezon.wifikey.wifi.info.BlackWifiInfo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/9/12.
 */

public class BlackWifiAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String DATA_FORMAT = "yyyy/MM/dd";
    private SimpleDateFormat dateFormat = new SimpleDateFormat(DATA_FORMAT);


    private Context mContext;
    private List<BlackWifiInfo> mData = new ArrayList<>();
    private BlackItemClickListener mBlackItemClickListener;

    public BlackWifiAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<BlackWifiInfo> data) {
        if (data != null) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }

    public void setListener(BlackItemClickListener listener) {
        if (listener != null) {
            this.mBlackItemClickListener = listener;
        }
    }

    public List<BlackWifiInfo> getData() {
        return mData;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new BlackWifiViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_black_wifi, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final BlackWifiInfo blackWifiInfo = mData.get(position);
        if (holder instanceof BlackWifiViewHolder) {
            final BlackWifiViewHolder viewHolder = (BlackWifiViewHolder) holder;
            viewHolder.tvBlackName.setText(blackWifiInfo.getSsid());
            viewHolder.tvDate.setText(dateFormat.format(new Date(blackWifiInfo.getTime())));
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mBlackItemClickListener != null) {
                        mBlackItemClickListener.onBlackItemClicked(blackWifiInfo);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public static class BlackWifiViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_blackname)
        TextView tvBlackName;
        @BindView(R.id.tv_date)
        TextView tvDate;

        public BlackWifiViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public static interface BlackItemClickListener {
        void onBlackItemClicked(BlackWifiInfo blackWifiInfo);
    }
}
